package com.rs.game.player.controlers;

import com.rs.Settings;
import com.rs.game.Entity;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.WorldTile;
import com.rs.game.item.Item;
import com.rs.game.npc.NPC;
import com.rs.game.npc.others.TutorialGuide;
import com.rs.game.player.content.Magic;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.SerializableFilesManager;

public class StartTutorial extends Controler {

	private static final int QUEST_GUIDE_NPC = 949;

	private int stageLong;
	public void addNPCHintIcon() {
		NPC guide = findNPC(QUEST_GUIDE_NPC);
		if (guide != null)
			player.getHintIconsManager().addHintIcon(guide, 0, -1, false);
		player.unlock();
	}

	@Override
	public boolean canAttack(Entity target) {
		return false;
	}

	@Override
	public boolean canHit(Entity target) {
		return false;
	}

	@Override
	public boolean canWalk() {
		if (getTutorialStage() == 1)
			return true;
		return false;
	}

	public NPC findNPC(int id) {
		for (NPC npc : World.getNPCs()) {
			if (npc == null || npc.getId() != id)
				continue;
			return npc;
		}
		return null;
	}

	public int getStage() {
		if (getArguments() == null)
			setArguments(new Object[] { 0 }); // index 0 = stage
		return (Integer) getArguments()[0];
	}

	public int getTutorialStage() {
		return stageLong;
	}

	@Override
	public boolean keepCombating(Entity target) {
		return false;
	}

	/*
	 * return remove controler
	 */
	@Override
	public boolean login() {
		start();
		return false;
	}

	/*
	 * return remove controler
	 */
	@Override
	public boolean logout() {
		Magic.HomeTeleport(player, 2193, 3184, 0);
		return true;
	}

	@Override
	public void process() {
	}

	@Override
	public boolean processItemTeleport(WorldTile toTile) {
		return false;
	}

	@Override
	public boolean processMagicTeleport(WorldTile toTile) {
		return false;
	}

	@Override
	public boolean processNPCClick1(NPC npc) {
		npc.faceEntity(player);
		switch (npc.getId()) {
		case 949:
			player.getDialogueManager().startDialogue("QuestGuide", QUEST_GUIDE_NPC, this);
			break;
		}
		return false;
	}

	@Override
	public boolean processObjectClick1(WorldObject object) {
		return false;
	}

	@Override
	public boolean processObjectClick2(WorldObject object) {
		return false;
	}

	@Override
	public boolean processObjectClick3(WorldObject object) {
		return false;
	}

	@Override
	public boolean processObjectTeleport(WorldTile toTile) {
		return false;
	}

	@Override
	public void sendInterfaces() {
		addNPCHintIcon();
		player.getInterfaceManager().replaceRealChatBoxInterface(372);
		player.getPackets().sendIComponentText(372, 0, "Getting Started");
		player.getPackets().sendIComponentText(372, 1,"To complete the tutorial talk to the");
		player.getPackets().sendIComponentText(372, 2,"Quest Guide in this room. He is indicated by a flashing");
		player.getPackets().sendIComponentText(372, 3,"yellow arrow above his head. If you can't see him use your");
		player.getPackets().sendIComponentText(372, 4,"keyboard arrow keys to rotate the view.");
		player.getPackets().sendIComponentText(372, 5, "");
		player.getPackets().sendIComponentText(372, 6, "");
	}

	public void setStage(int stage) {
		getArguments()[0] = stage;
	}

	public void setTutorialStage(int stageLong) {
		switch (stageLong) {
		case 1:
			player.getAppearence().switchHidden();
			player.lock();
			new TutorialGuide(new WorldTile(2188, 3182, 0), player, this);
			player.setNextWorldTile(new WorldTile(2188, 3181, 0));
			player.getDialogueManager().startDialogue("SimpleMessage", "This is our home. It's the center of Styx. Here, you can find the Teleporter for Transportation,"
					+ " or the Grand-Exchange to buy-sell your most valuable items. And if you can't wait for someone to sell-buy goods, then there are a few shops to supply you. ");
			WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					setTutorialStage(2);
				}
			}, 20);
			break;
		case 2:
			player.setNextWorldTile(new WorldTile(3450, 3723, 0));
			new TutorialGuide(new WorldTile(3450, 3724, 0), player, this);
			player.getDialogueManager().startDialogue("SimpleMessage", "This is the Dungeoneering Area. You can simply do ::dung to teleport. Click on the the Dungeoneering entrance and choose the size of your Dungeon. Then you just have to find "
					+ "the boss and kill it. You will be rewarded with Dungeoneering tokens and experience for completing the Dungeon. You can spend those at the Reward trader for Chaotic Weaponry & more!");
			WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					setTutorialStage(3);
				}
			}, 20);
			break;
		case 3: 
			player.setNextWorldTile(new WorldTile(2494, 2915, 0));
			new TutorialGuide(new WorldTile(2494, 2916, 0), player, this);
			player.getDialogueManager().startDialogue("SimpleMessage", "Hunter. One of the most profitable skilling methods available. Most likely due to the red chinchompa's. Red chinchompa's are desired by a lot of players due to their ability to multi-hit and greater XP-reward. Talk to the Teleporter at home to get here and run west.");
			WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					updateProgress();
				}
			}, 15);
			break;
		}

		this.stageLong = stageLong;
	}

	@Override
	public void start() {
		new TutorialGuide(new WorldTile(3552, 4976, 0), player, this);
		player.getInterfaceManager().closeQuests();
		player.setYellOff(true);
		player.getPackets().sendMusicEffect(13);
		sendInterfaces();
	}

	public void updateProgress() {
		player.getAppearence().switchHidden();
		player.unlock();
		player.getInterfaceManager().removeAll();
		player.getDialogueManager().startDialogue("SimpleMessage", "Congratulations! You finished the Starter Tutorial and you are rewarded with 2.5M.");
		player.getHintIconsManager().removeUnsavedHintIcon();
		player.getMusicsManager().reset();
		//player.getInterfaceManager().sendQuests();
		player.getInventory().addItemMoneyPouch(new Item(995, 2500000));
		World.sendWorldMessage("<col=FF0000> All welcome "+player.getDisplayName()+" to "+Settings.SERVER_NAME+"!", true);
		player.setNextWorldTile(Settings.RESPAWN_PLAYER_LOCATION);
		player.setYellOff(false);
		WorldTasksManager.schedule(new WorldTask() {
			@Override
			public void run() {
				player.getInterfaceManager().sendInterfaces();
				player.getInterfaceManager()
				.closeReplacedRealChatBoxInterface();
				removeControler();
			}
		});
		SerializableFilesManager.savePlayer(player);
	}

}